#!/bin/bash
# 验证 direnv + FVM 配置是否正确

echo "🔍 Camera SDK 环境验证"
echo "====================="
echo ""

ERRORS=0
WARNINGS=0

# 检查 direnv
echo "📦 检查 direnv..."
if command -v direnv &> /dev/null; then
    echo "✅ direnv 已安装: $(direnv version)"
else
    echo "❌ direnv 未安装"
    ((ERRORS++))
fi
echo ""

# 检查 shell hook
echo "📦 检查 shell hook..."
SHELL_NAME="$(basename $SHELL)"
SHELL_RC=""

case "$SHELL_NAME" in
    zsh)
        SHELL_RC="$HOME/.zshrc"
        ;;
    bash)
        SHELL_RC="$HOME/.bashrc"
        ;;
esac

if [ -n "$SHELL_RC" ] && [ -f "$SHELL_RC" ]; then
    if grep -q "direnv hook" "$SHELL_RC"; then
        echo "✅ direnv hook 已配置在 $SHELL_RC"
    else
        echo "⚠️  direnv hook 未配置在 $SHELL_RC"
        ((WARNINGS++))
    fi
else
    echo "⚠️  无法检查 shell 配置"
    ((WARNINGS++))
fi
echo ""

# 检查 FVM
echo "📦 检查 FVM..."
if command -v fvm &> /dev/null; then
    echo "✅ FVM 已安装"
    
    # 检查 Flutter 3.27.4
    if fvm list | grep -q "3.27.4"; then
        echo "✅ Flutter 3.27.4 已安装"
    else
        echo "❌ Flutter 3.27.4 未安装"
        ((ERRORS++))
    fi
else
    echo "❌ FVM 未安装"
    ((ERRORS++))
fi
echo ""

# 检查 .fvm 目录
echo "📦 检查 .fvm 配置..."
if [ -d ".fvm" ]; then
    echo "✅ .fvm 目录存在"
    
    if [ -f ".fvm/fvm_config.json" ]; then
        echo "✅ fvm_config.json 存在"
        if grep -q "3.27.4" ".fvm/fvm_config.json"; then
            echo "✅ 配置版本: 3.27.4"
        else
            echo "⚠️  版本配置可能不正确"
            ((WARNINGS++))
        fi
    else
        echo "⚠️  fvm_config.json 不存在"
        ((WARNINGS++))
    fi
    
    if [ -L ".fvm/flutter_sdk" ] || [ -d ".fvm/flutter_sdk" ]; then
        echo "✅ flutter_sdk 链接存在"
    else
        echo "❌ flutter_sdk 链接不存在"
        ((ERRORS++))
    fi
else
    echo "❌ .fvm 目录不存在"
    ((ERRORS++))
fi
echo ""

# 检查 .envrc 文件
echo "📦 检查 .envrc 文件..."
MODULES=(
    "."
    "packages/core/device_api"
    "packages/core/device_detector_api"
    "packages/core/photo_pipeline"
    "packages/core/pipeline_repository"
    "packages/core/repository_api"
    "packages/device/gphoto2_ffi"
    "packages/device/gphoto2_wrapper"
    "packages/device/ptp_wrapper"
    "packages/device/usb_detector"
    "packages/device/wifi_detector"
    "packages/upload/upload_manager"
    "packages/upload/upload_service"
    "packages/ai_pickpic/pickpic_engine"
    "packages/ai_pickpic/pickpic_task_runner"
    "packages/core/task_executor"
    "examples/photo_pipeline_example"
    "examples/photo_pipeline_example"
)

ENVRC_COUNT=0
for module in "${MODULES[@]}"; do
    if [ -f "$module/.envrc" ]; then
        ((ENVRC_COUNT++))
    else
        echo "⚠️  $module/.envrc 不存在"
        ((WARNINGS++))
    fi
done

echo "✅ 找到 $ENVRC_COUNT / ${#MODULES[@]} 个 .envrc 文件"
echo ""

# 检查 direnv 授权状态
echo "📦 检查 direnv 授权..."
if [ -f ".envrc" ]; then
    if direnv status 2>/dev/null | grep -q "Found RC allowed true"; then
        echo "✅ 主项目 .envrc 已授权"
    else
        echo "⚠️  主项目 .envrc 可能未授权，请运行: direnv allow"
        ((WARNINGS++))
    fi
fi
echo ""

# 检查 Flutter 命令
echo "📦 检查 Flutter 可用性..."
if command -v fvm &> /dev/null; then
    FLUTTER_VERSION=$(fvm flutter --version 2>&1 | head -n 1)
    if echo "$FLUTTER_VERSION" | grep -q "3.27.4"; then
        echo "✅ Flutter 版本正确: $FLUTTER_VERSION"
    else
        echo "⚠️  Flutter 版本: $FLUTTER_VERSION"
        echo "   期望版本: 3.27.4"
        ((WARNINGS++))
    fi
    
    # 检查 Flutter 路径
    FLUTTER_PATH=$(fvm which flutter 2>/dev/null)
    if [ -n "$FLUTTER_PATH" ] && echo "$FLUTTER_PATH" | grep -q ".fvm"; then
        echo "✅ Flutter 路径正确（通过 FVM）"
        echo "   路径: $FLUTTER_PATH"
    else
        echo "⚠️  无法确认 FVM Flutter 路径"
        if [ -n "$FLUTTER_PATH" ]; then
            echo "   当前路径: $FLUTTER_PATH"
        fi
        ((WARNINGS++))
    fi
else
    echo "❌ fvm flutter 命令不可用"
    ((ERRORS++))
fi
echo ""

# 检查文档
echo "📦 检查文档..."
DOCS=(
    "README.md"
    "ARCHITECTURE.md"
    "DIRENV_SETUP.md"
    "DIRENV_SUMMARY.md"
)

for doc in "${DOCS[@]}"; do
    if [ -f "$doc" ]; then
        echo "✅ $doc 存在"
    else
        echo "⚠️  $doc 不存在"
        ((WARNINGS++))
    fi
done
echo ""

# 总结
echo "📊 验证总结"
echo "=========="
if [ $ERRORS -eq 0 ] && [ $WARNINGS -eq 0 ]; then
    echo "🎉 完美！所有检查都通过了！"
    echo ""
    echo "✨ 您的环境已经准备就绪！"
    echo ""
    echo "📝 下一步:"
    echo "  1. 进入任何子模块: cd camera_sdk"
    echo "  2. 运行 Flutter 命令: fvm flutter pub get"
    echo "  3. 开始开发！"
elif [ $ERRORS -eq 0 ]; then
    echo "⚠️  有 $WARNINGS 个警告，但配置基本可用"
    echo ""
    echo "💡 建议查看上面的警告信息并修复"
else
    echo "❌ 发现 $ERRORS 个错误，$WARNINGS 个警告"
    echo ""
    echo "🔧 请运行设置脚本修复问题:"
    echo "   ./setup.sh"
    echo ""
    echo "或查看文档手动配置:"
    echo "   - DIRENV_SETUP.md"
fi
echo ""

exit $ERRORS

